class Solution25 {
    public boolean[][] row;
    public boolean[][] col;
    public boolean[][][] grid;
    public void solveSudoku(char[][] board) {
        row = new boolean[9][10];
        col = new boolean[9][10];
        grid = new boolean[3][3][10];
        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                if(board[i][j] != '.') {
                    int num = board[i][j] - '0';
                    if(row[i][num] == false && col[j][num] == false && grid[i/3][j/3][num] == false) {
                        row[i][num] = col[j][num] = grid[i/3][j/3][num] = true;
                    }
                }
            }
        }
        dfs(board);
    }

    public boolean dfs(char[][] board) {
        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                if(board[i][j] == '.') {
                    for(int n = 1; n < 10; n++) {
                        if(row[i][n] == false && col[j][n] == false && grid[i/3][j/3][n] == false) {
                            board[i][j] = (char) (n + '0');
                            row[i][n] = col[j][n] = grid[i/3][j/3][n] = true;
                            if(dfs(board) == true) return true;
                            board[i][j] = '.';
                            row[i][n] = col[j][n] = grid[i/3][j/3][n] = false;
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }
}